//
//  JiaoYiZheTableViewCell.m
//  DBAChain
//
//  Created by Evan on 2018/9/6.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "JiaoYiZheTableViewCell.h"
#import "CommonData.h"
#import <Masonry.h>

@implementation JiaoYiZheTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath
{
    NSString *Service = [NSString stringWithFormat:@"JiaoYiZheTableViewCell_%ld_%ld",(long)indexPath.section,(long)indexPath.row];
    JiaoYiZheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Service];
    if (cell == nil) {
        [tableView registerClass:[JiaoYiZheTableViewCell class] forCellReuseIdentifier:Service];
        cell = [tableView dequeueReusableCellWithIdentifier:Service];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    return cell;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

-(void)initUI{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    [self viewBg];
    
    [self labelCNY];
    [self labelUp];
}

-(UIView *)viewBg{
    if (!_viewBg) {
        _viewBg = [[UIView alloc]init];
        [self addSubview:_viewBg];
        [_viewBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(15);
            make.top.mas_equalTo(self).offset(15);
            make.right.mas_equalTo(self).offset(-15);
            make.bottom.mas_equalTo(self).offset(-15);
        }];
        _viewBg.backgroundColor = [UIColor whiteColor];
        _viewBg.layer.cornerRadius = 10;
        
        _viewBg.layer.shadowColor = [UIColor blackColor].CGColor;
        //        阴影的透明度
        _viewBg.layer.shadowOpacity = 0.3f;
        //        阴影的圆角
        _viewBg.layer.shadowRadius = 4.0f;
    }
    return _viewBg;
}


-(UILabel*)labelCNY{
    if (!_labelCNY) {
        _labelCNY = [[UILabel alloc]init];
        [self.viewBg addSubview:_labelCNY];
        [_labelCNY mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.viewBg).offset(15);
            make.top.mas_equalTo(self.viewBg).offset(10);
            make.height.offset(20);
            make.width.offset(KScreenWidth);
        }];
        _labelCNY.text = @"当前价格：1.1 CNY";
        _labelCNY.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
        _labelCNY.textColor = COLOR_main;
    }
    return _labelCNY;
}


-(UILabel*)labelUp{
    if (!_labelUp) {
        _labelUp = [[UILabel alloc]init];
        [self.viewBg addSubview:_labelUp];
        [_labelUp mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.viewBg).offset(15);
            make.top.mas_equalTo(self.labelCNY.mas_bottom).offset(10);
            make.height.offset(20);
            make.width.offset(200);
        }];
        _labelUp.text = @"涨幅：";
        _labelUp.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
        _labelUp.textColor = LCFromRGB(165, 165, 165);
    }
    return _labelUp;
}


#pragma mark - 折线图


-(AAChartView*)aaChartView{
    if (!_aaChartView) {
        _aaChartView = [[AAChartView alloc]initWithFrame:CGRectMake(10, 65, KScreenWidth - 50 , 300)];
        _aaChartView.backgroundColor = [UIColor clearColor];
        _aaChartView.contentHeight =300;
        _aaChartView.isClearBackgroundColor = YES;
        [self.viewBg addSubview:_aaChartView];
    }
    return _aaChartView;
}


-(void)initData:(NSArray< dataZheXianInfoList*> *) orderList{
    //    if([orderList count] != 9) return;
    
    NSMutableArray *arrayPrice = [[NSMutableArray alloc]init];
    NSMutableArray *arrayDate = [[NSMutableArray alloc]init];
    
    //    for (NSInteger i = ([orderList count] -1); i >= 0; i--) {
    
    for (NSInteger i = 0; i < [orderList count]; i++) {
        
        dataZheXianInfoList *mm = [dataZheXianInfoList mj_objectWithKeyValues:[orderList objectAtIndex:i]];
        NSString* dateStr = mm.chinaAdate;
        
//        NSDateFormatter* formater = [[NSDateFormatter alloc] init];
//        [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//        formater.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
//        NSDate* date = [formater dateFromString:dateStr];
//        [formater setDateFormat:@"MM-dd"];
//        NSString *str = [formater stringFromDate:date];
        [arrayDate addObject:dateStr];
        [arrayPrice addObject:@(mm.chinaARMB)];
    }
    
    
    AAChartModel *chartModel= AAObject(AAChartModel)
    .chartTypeSet(AAChartTypeSpline)//设置图表的类型(这里以设置的为柱状图为例)
    .titleSet(@"chinaA每日CNY价格")//设置图表标题
    .subtitleSet(@"价格")//设置图表副标题
    .categoriesSet(arrayDate)//@[@"Java",@"Swift",@"Python",@"Ruby", @"PHP",@"Go",@"C",@"C#",@"C++"])//设置图表横轴的内容
    .yAxisTitleSet(@"CNY")//设置图表 y 轴的单位
    .seriesSet(@[
                 AAObject(AASeriesElement)
                 .nameSet(@"chinaA")
                 .dataSet(arrayPrice),//@[@45,@56,@34,@43,@65,@56,@47,@28,@49]),
                 //                 AAObject(AASeriesElement)
                 //                 .nameSet(@"GTE")
                 //                 .dataSet(@[@11,@12,@13,@14,@15,@16,@17,@18,@19]),
                 //                 AAObject(AASeriesElement)
                 //                 .nameSet(@"2019")
                 //                 .dataSet(@[@31,@22,@33,@54,@35,@36,@27,@38,@39]),
                 //                 AAObject(AASeriesElement)
                 //                 .nameSet(@"2020")
                 //                 .dataSet(@[@21,@22,@53,@24,@65,@26,@37,@28,@49]),
                 ])
    ;
    
    
    /*图表视图对象调用图表模型对象,绘制最终图形*/
    [self.aaChartView aa_drawChartWithChartModel:chartModel];
    [self.aaChartView aa_refreshChartWithChartModel:chartModel];
}


@end
